
#include "config.h"

#define IOx_SP_KR00  GPIOB ,11u
#define IOx_SP_KR01  GPIOB ,12u
#define IOx_SP_KR02  GPIOB ,12u
#define IOx_SP_KR03  GPIOB ,14u
#define IOx_SP_KR04  GPIOA ,15u
#define IOx_SP_KR05  GPIOD ,0u
#define IOx_SP_KR06  GPIOD ,1u
#define IOx_SP_KR07  GPIOD ,2u
#define IOx_SP_KR08  GPIOD ,3u
#define IOx_SP_KR09  GPIOB ,3u
#define IOx_SP_KR10  GPIOB ,4u
#define IOx_SP_KR11  GPIOB ,5u


#define KR00_state_SP IF_I_H_(IOx_SP_KR00)  
#define KR01_state_SP IF_I_H_(IOx_SP_KR01)  
#define KR02_state_SP IF_I_H_(IOx_SP_KR02)  
#define KR03_state_SP IF_I_H_(IOx_SP_KR03)  
#define KR04_state_SP IF_I_H_(IOx_SP_KR04)  
#define KR05_state_SP IF_I_H_(IOx_SP_KR05)  
#define KR06_state_SP IF_I_H_(IOx_SP_KR06)  
#define KR07_state_SP IF_I_H_(IOx_SP_KR07)  
#define KR08_state_SP IF_I_H_(IOx_SP_KR08)  
#define KR09_state_SP IF_I_H_(IOx_SP_KR09)  
#define KR10_state_SP IF_I_H_(IOx_SP_KR10)  
#define KR11_state_SP IF_I_H_(IOx_SP_KR11)  


#define IOx_KC1_Z GPIOA,05U
#define IOx_KC2_Z GPIOA,07U
#define IOx_KC3_Z GPIOB,01U
#define IOx_KC4_Z GPIOB,10U

#define IOx_KC1_F GPIOA,04U
#define IOx_KC2_F GPIOA,06U
#define IOx_KC3_F GPIOB,00U
#define IOx_KC4_F GPIOB,02U

#define KC1_OFF  { SET_IO_L_(IOx_KC1_Z);SET_IO_H_(IOx_KC1_F); }
#define KC2_OFF  { SET_IO_L_(IOx_KC2_Z);SET_IO_H_(IOx_KC2_F); }
#define KC3_OFF  { SET_IO_L_(IOx_KC3_Z);SET_IO_H_(IOx_KC3_F); }
#define KC4_OFF  { SET_IO_L_(IOx_KC4_Z);SET_IO_H_(IOx_KC4_F); }

#define KC1_ON   { SET_IO_H_(IOx_KC1_Z);SET_IO_L_(IOx_KC1_F); }
#define KC2_ON   { SET_IO_H_(IOx_KC2_Z);SET_IO_L_(IOx_KC2_F); }
#define KC3_ON   { SET_IO_H_(IOx_KC3_Z);SET_IO_L_(IOx_KC3_F); }
#define KC4_ON   { SET_IO_H_(IOx_KC4_Z);SET_IO_L_(IOx_KC4_F); }

#define KC1_STE  IF_O_H_(IOx_KC1_Z)
#define KC2_STE  IF_O_H_(IOx_KC2_Z)
#define KC3_STE  IF_O_H_(IOx_KC3_Z)
#define KC4_STE  IF_O_H_(IOx_KC4_Z)


static inline void IO_INIT_SP1(void)
{
	LED_IO_INIT();
	One_PIN_init_(IOx_SP_KR00, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR01, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR02, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR03, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR04, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR05, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR06, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR07, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR08, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR09, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR10, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_SP_KR11, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);

	KC1_OFF;
	KC2_OFF;
	KC3_OFF;
	KC4_OFF;
	One_PIN_init_(IOx_KC1_Z, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_KC2_Z, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_KC3_Z, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_KC4_Z, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_KC1_F, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_KC2_F, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_KC3_F, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
	One_PIN_init_(IOx_KC4_F, LL_GPIO_MODE_INPUT, LL_GPIO_SPEED_MEDIUM, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO);
}


/**
*@author     2025.10.29 WangXQ
*@brief      
*@param      void
*@return     void
**/
void Deal_SP(void)
{
	IO_INIT_SP1();
	SysTick_init(1000);
	COM1_Init(115200, WordL_8, StopBits_1, Parity_no, 0);
	COM2_Init(115200, WordL_8, StopBits_1, Parity_no, 0);
	printf_my("\r\n%s_SP, HARD_FLAG:%02X", Version, HARD_FLAG);
	while (1)
	{
// 		printf_my("\r\n^M^");
// 		u32 BitF = 0x01;
// 		for (u8 i = 0; i < 20; i++)
// 		{
// 			if (BitF & Data_Buf[0]) printf_my(" 1 ");
// 			else  				    printf_my(" 0 ");
// 			BitF <<= 1;
// 		}
		OSTimeDly(ms2000);
	}
}
/**
*@author     2025.10.29 WangXQ
*@brief      
*@param      void
*@return     void
**/
void SysTick_Handler_SP_1mA(void)
{
	static u8 T_10ms = 0;

	relay_out_deal_1ms(0,KC1_ON, KC1_OFF,KC1_STE);
	relay_out_deal_1ms(1,KC2_ON, KC2_OFF,KC2_STE);
	relay_out_deal_1ms(2,KC3_ON, KC3_OFF,KC3_STE);
	relay_out_deal_1ms(2,KC4_ON, KC4_OFF,KC4_STE);

	switch (T_10ms++)
	{
	case 0:	T_10ms = 1; KR_LB_DEAL_00_15(0, KR00_state_SP);  KR_LB_DEAL_00_15(10, KR10_state_SP); break;
	case 1:	T_10ms = 2; KR_LB_DEAL_00_15(1, KR01_state_SP);  KR_LB_DEAL_00_15(11, KR11_state_SP); break;
	case 2: T_10ms = 3; KR_LB_DEAL_00_15(2, KR02_state_SP);                                       break;
	case 3:	T_10ms = 4; KR_LB_DEAL_00_15(3, KR03_state_SP);                                       break;
	case 4: T_10ms = 5; KR_LB_DEAL_00_15(4, KR04_state_SP);                                       break;
	case 5:	T_10ms = 6; KR_LB_DEAL_00_15(5, KR05_state_SP);                                       break;
	case 6:	T_10ms = 7; KR_LB_DEAL_00_15(6, KR06_state_SP);                                       break;
	case 7:	T_10ms = 8; KR_LB_DEAL_00_15(7, KR07_state_SP);                                       break;
	case 8:	T_10ms = 9; KR_LB_DEAL_00_15(8, KR08_state_SP);                                       break;
	default:T_10ms = 0; KR_LB_DEAL_00_15(9, KR09_state_SP);
	{
		static u16 T_LED_2S = 0;
		if (++T_LED_2S == 85+150) { T_LED_2S = 0; }

		if (    T_LED_2S < 10 
			|| ((35  < T_LED_2S) && (T_LED_2S <  45)) 
			|| ((70  < T_LED_2S) && (T_LED_2S <  85))
//			|| ((105 < T_LED_2S) && (T_LED_2S < 115))
			)
		{
			LED0_ON;
		}
		else
		{
			LED0_OF;
		}
	}		
	break;
	}
}


